package jp.co.ziro.wikidozan.controller.admin;

import jp.co.ziro.wikidozan.model.View;
import jp.co.ziro.wikidozan.service.ViewService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

public class RegistViewController extends Controller {

    @Override
    public Navigation run() throws Exception {
        
        String viewName = requestScope("viewName");
        viewName = viewName.replaceAll("_", "/");
        viewName = "." + viewName + ".xml";
  
        String viewText = requestScope("viewText");
        
        ViewService service = new ViewService();
        View viewData = service.findView(viewName);
        
        if ( viewData == null ) {
            viewData = new View();
            viewData.setKey(Datastore.allocateId(View.class));
            viewData.setName(viewName);
        }
        
        viewData.setText(viewText);
        Datastore.put(viewData);

        return forward("view.jsp");
    }
}
